package com.changan.controller;

import com.changan.entity.Role;
import com.changan.entity.User;
import com.changan.service.RoleService;
import com.changan.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/hello")
    public String hello(){
        User user = userService.selectByPrimaryKey(1l);
        System.out.println(user.toString());
        return "index";
    }

    @RequestMapping("/getUsersByPage")
    public String getUsersByPage(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize",required = true,defaultValue = "5") Integer pageSize,
                                 @RequestParam(value = "userName",required = false) String userName,
                                 @RequestParam(value = "userRole",required = false) Integer userRole,
                                 Model model){
        PageInfo<User> pageInfo = userService.getUsersByPage(pageNum,pageSize,userName,userRole);
        List<Role> roleList = roleService.getRoleList();
        model.addAttribute("roleList",roleList);
        model.addAttribute("pageInfo",pageInfo);
        return "user";
    }

    @RequestMapping("/getById/{id}")
    public String getById(@PathVariable("id") Long id,Model model){
        User user = userService.selectByPrimaryKey(id);
        System.out.println(user.toString());
        model.addAttribute("user",user);
        return "user";
    }

}
